package org.market.domain.strategy.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.market.domain.strategy.model.entity.RaffleAwardEntity;
import org.market.domain.strategy.model.entity.RaffleFactorEntity;
import org.market.domain.strategy.repository.IStrategyRepository;
import org.market.domain.strategy.service.armory.IStrategyDispatch;
import org.market.domain.strategy.service.raffle.IRaffleStock;
import org.market.domain.strategy.service.raffle.IRaffleStrategy;
import org.market.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.market.domain.strategy.service.rule.tree.factory.DefaultRuleTreeFactory;
import org.market.types.enums.ResponseCode;
import org.market.types.exception.AppException;
import org.springframework.stereotype.Service;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/8 下午9:56
 * @Version: 1.0
 */

@Slf4j
@Service
public abstract class AbstractRaffleStrategy implements IRaffleStrategy, IRaffleStock {

    //仓储策略 ---> domain层就像大厨,仓储层就像材米油盐
    protected IStrategyRepository strategyRepository;
    //调度策略 ---> 只负责抽奖处理,通过新增接口的方式,职责隔离,不需要使用方关心接口的初始化
    protected IStrategyDispatch strategyDispatch;
    protected DefaultChainFactory chainFactory;
    protected DefaultRuleTreeFactory ruleTreeFactory;

    public AbstractRaffleStrategy(IStrategyRepository strategyRepository, IStrategyDispatch strategyDispatch, DefaultChainFactory chainFactory, DefaultRuleTreeFactory ruleTreeFactory) {
        this.strategyRepository = strategyRepository;
        this.strategyDispatch = strategyDispatch;
        this.chainFactory = chainFactory;
        this.ruleTreeFactory = ruleTreeFactory;
    }

    @Override
    public RaffleAwardEntity performRaffle(RaffleFactorEntity raffleFactorEntity) {
        //1.参数校验
        Long strategyId = raffleFactorEntity.getStrategyId();
        String userId = raffleFactorEntity.getUserId();
        if (strategyId == null || StringUtils.isBlank(userId) ) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(),ResponseCode.ILLEGAL_PARAMETER.getCode());
        }

        DefaultChainFactory.StrategyAwardVO chainAwardVO = logicChain(userId, strategyId);
        Integer awardId = chainAwardVO.getAwardId();
        String awardRuleVale = chainAwardVO.getAwardRuleVale();
        log.info("抽奖策略-责任链,请求参数: userId:{},strategyId:{},awardId:{}",userId,strategyId,awardId);
        if (!awardRuleVale.equals(DefaultChainFactory.logicMode.RULE_DEFAULT.getCode())){
            return RaffleAwardEntity.builder()
                    .awardId(awardId)
                    .build();
        }
        log.info("抽奖策略-规则树开始");
        DefaultRuleTreeFactory.StrategyAwardVO ruleTreeAwardVO = loginRuleTree(userId, strategyId, awardId);
        log.info("抽奖策略-规则树完成,请求参数: userId:{},strategyId:{},awardId:{}",userId,strategyId,awardId);

        /*//2.查询策略实体,准备进行抽奖操作
        StrategyEntity strategyEntity = strategyRepository.queryStrategyEntityByStrategyId(strategyId);
        //3.执行模板方法,完成抽奖前的过滤
        RuleActionEntity<RuleActionEntity.BeforeRaffleActionEntity> raffleAfterFilter =
                this.checkBeforeRaffle(RaffleFactorEntity.builder().strategyId(strategyId).userId(userId).build(),strategyEntity.RuleModels());

        //如果返回为接管的信息
        if (RuleLogicCheckTypeVO.TAKE_OVER.getCode().equals(raffleAfterFilter.getCode())){
            if (raffleAfterFilter.getCode().equals(DefaultLogicFactory.LogicModel.RULE_BLACKLIST.getCode())){
                //给黑名单返回对应的值
                return RaffleAwardEntity
                        .builder()
                        .awardId(raffleAfterFilter.getData().getAwardId())
                        .build();
            } else if (raffleAfterFilter.getCode().equals(DefaultLogicFactory.LogicModel.RULE_WEIGHT.getCode())) {
                //根据积分权重返回对应值
                
                //获取积分权重
                RuleActionEntity.BeforeRaffleActionEntity beforeRaffleAction = raffleAfterFilter.getData();
                String ruleWeightValueKey = beforeRaffleAction.getRuleWeightValueKey();
                Integer awardId = strategyDispatch.getRandomAwardId(raffleFactorEntity.getStrategyId(),ruleWeightValueKey);
                return RaffleAwardEntity.builder()
                        .awardId(awardId)
                        .build();
            }
        }
*/
        /*        ILogicChain logicChain = chainFactory.buildLogicChain(strategyId);
        Integer awardId = logicChain.logic(userId, strategyId);


        //过滤抽奖次数是否满足
        StrategyAwadRuleModelVO strategyAwadRuleModelVO = strategyRepository.queryStrategyAwardRuleModel(strategyId,awardId);
        RuleActionEntity<RuleActionEntity.DuringRaffleActionEntity> afterDuringFilter = this.checkDuringRaffle(RaffleFactorEntity.builder().awardId(awardId).strategyId(strategyId).userId(userId).build(), strategyAwadRuleModelVO.duringRaffleRuleList());

        if (RuleLogicCheckTypeVO.TAKE_OVER.getCode().equals(afterDuringFilter.getCode())){
            return RaffleAwardEntity.builder()
                    .awardDesc("[规则中日志:]奖品未解锁!!!")
                    .build();
        }

        log.info("[规则中日志:]奖品解锁~~~");*/

        return RaffleAwardEntity.builder()
                .awardId(ruleTreeAwardVO.getAwardId())
                .awardDesc(ruleTreeAwardVO.getAwardRuleValue())
                .build();
    }

    public abstract DefaultChainFactory.StrategyAwardVO logicChain(String userId,Long StrategyId);
    public abstract DefaultRuleTreeFactory.StrategyAwardVO loginRuleTree(String userId,Long StrategyId,Integer awardId);
}
